import Controller from './Controller';
import NumberController from './NumberController';

export default class VectorController extends Controller {

	constructor( parent, object, property , subKeys,min, max, step) {

		super( parent, object, property, 'number', 'label' );

		this.isVectorController = true ;
		this.$span = document.createElement("span") ;

		this.$obj = object[property] ;
		let config = {autoAddDom:false,className:"vector"} ;
		if(subKeys == null || subKeys == "") subKeys = "x,y,z" ;
		let numArray = subKeys.split(",") ;
		this.$array = [] ;
		for(let i=0; i<numArray.length; i++){
			let key = numArray[i] ;
			let numController = new NumberController(parent,this.$obj,key,min, max, step,config);
			//不显示滚动
			numController.sliderEnable = false ;
			numController.$eventController = this ;
			this.$span.appendChild(numController.domElement) ;
			this.$array.push(numController) ;
		}

		this.$widget.appendChild( this.$span );

		this.updateDisplay();

	}

	updateDisplay() {
		for(let i=0; i<this.$array.length; i++){
			this.$array[i].updateDisplay() ;
		}
		return this;
	}

	min( min ) {
		for(let i=0; i<this.$array.length; i++){
			this.$array[i].min(min) ;
		}
		return this;
	}

	max( max ) {
		for(let i=0; i<this.$array.length; i++){
			this.$array[i].max(max) ;
		}
		return this;
	}

	step( step, explicit = true ) {
		for(let i=0; i<this.$array.length; i++){
			this.$array[i].step(step, explicit) ;
		}
		return this;
	}

	decimals( decimals ) {
		for(let i=0; i<this.$array.length; i++){
			this.$array[i].decimals(decimals) ;
		}
		return this;
	}

	tooltip2(strArray) {
		if(strArray == null) return this;
		for(let i=0; i<this.$array.length; i++){
			if(strArray.length > i)
				this.$array[i].tooltip(strArray[i]) ;
		}
		return this;
	}

	onChange( callback ) {
		let that = this ;
		this._onChange = callback;

		for(let i=0; i<this.$array.length; i++){
			this.$array[i]._onChange = function(value){
				callback.call( this, that.$obj,that);
			} ;
		}
		return this;
	}

	onFinishChangeFirst( callback ) {
		let that = this ;
		this._onFinishChangeFirst = callback;
		for(let i=0; i<this.$array.length; i++){
			let _controller = this.$array[i] ;
			_controller._onFinishChangeFirst = function(value){
				callback.call( _controller, that.$obj,that,_controller);
			} ;
		}

		return this;
	}

	onFinishChange( callback ) {
		let that = this ;
		this._onFinishChange = callback;
		for(let i=0; i<this.$array.length; i++){
			let _controller = this.$array[i] ;
			_controller._onFinishChange = function(value){
				callback.call( _controller, that.$obj,that,_controller);
			} ;
		}

		return this;
	}
}
